package backpage.controller;


import backpage.common.Result;
import backpage.entity.po.CompoffApply;
import backpage.entity.vo.CompoffApplyVo;
import backpage.service.ICompoffApplyService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  调休控制器
 * </p>
 *
 * @author 刘仁杰
 * @since 2025-04-19
 */
@Slf4j
@RestController
@RequestMapping("/compoff")
public class CompoffApplyController {

    @Autowired
    private ICompoffApplyService compoffApplyService;

    //保存申请 但不提交
    @PostMapping("/apply/add")
    public Result<?> addCompoffApply(@RequestBody @Valid CompoffApply compoffApply) {
        compoffApplyService.addCompoffApply(compoffApply);
        return Result.success("申请保存成功");
    }

    //删除申请
    @DeleteMapping("/apply/delete/{id}")
    public Result<?> deleteCompoffApply(@PathVariable Long id) {
        log.info("正在使用");
        compoffApplyService.deleteById(id);
        return Result.success("删除成功");
    }

    //提交申请
    @PostMapping("/apply/save")
    public Result<?> saveCompoffApply(@RequestBody @Valid CompoffApply compoffApply) {
        compoffApplyService.saveCompoffApply(compoffApply);
        return Result.success("申请提交成功");
    }


    //通过employId获取自己的申请记录
    @GetMapping("/list/employ")
    public Result<?> listCompoffApplyEmploy(
            @RequestParam(required = false) Integer employId,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            @RequestParam(required = false) Integer state,
            @RequestParam(required = false) Integer approveId,
            @RequestParam int pageNum,
            @RequestParam int pageSize) {
        PageInfo<CompoffApplyVo> listByEmployId = compoffApplyService.getListByEmployIdAndParam(
                employId, startTime, endTime, state,approveId, pageNum, pageSize
        );
        return Result.success(listByEmployId);
    }

    @GetMapping("/list/approver")
    public Result<?> listCompoffApplyApprover(
            @RequestParam Integer approverId,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            @RequestParam(required = false) Integer state,
            @RequestParam int pageNum,
            @RequestParam int pageSize) {

        PageInfo<CompoffApply> listByApproverId = compoffApplyService.getListByApproverIdAndParam(
                approverId, startTime, endTime, state, pageNum, pageSize
        );
        return Result.success(listByApproverId);
    }


    //审批申请
    @PostMapping("/apply/allow")
    public Result<?> allowCompoffApply(@RequestBody @Valid CompoffApply compoffApply) {
        compoffApplyService.appoverCompoffApply(compoffApply);
        return Result.success("审批成功");
    }

    @PatchMapping("/apply/allowQuick")
    public Result<?> quickApproveOvertimeApply(@RequestBody Map<String, Object> updateFields) {
        // 提取申请ID
        Long id = Long.valueOf(updateFields.get("id").toString());

        // 提取要更新的状态（假设前端传递的字段名为"state"）
        Integer state = (Integer) updateFields.get("state");

        // 调用服务层进行部分更新
        compoffApplyService.quickApproveOvertimeApply(id, state);

        return Result.success("快速审批成功");
    }


    @GetMapping("/daisize")
    public Result<?> daisizeCompoffApply(@RequestParam Integer approveId) {
        Integer size=compoffApplyService.getDaichuli(approveId);
        return Result.success(size);
    }

}
